package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Orders;
import com.gaohe.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/order")
public class OderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    HttpServletRequest request;

    //展示订单分页信息
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = orderService.pageOder(page, pageSize);
        return R.success(ordersPage);
    }

    //提交订单信息
    @PostMapping("/submit")
    public R<String> submitOrders(@RequestBody Orders orders) {
        String s = orderService.subOrders(orders);
        return R.success(s);
    }

    //管理端的分页展示
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, Long number, String beginTime, String endTime) {
        Page<Orders> ordersPage = orderService.ORDERS_PAGE(page, pageSize, number, beginTime, endTime);
        return R.success(ordersPage);
    }

    //派送状态更改
    @PutMapping
    public R<String> sendOrder(@RequestBody Orders orders) {
        //先查询再修改
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId, orders.getId());
        Orders order = orderService.getOne(lqw);
        order.setStatus(orders.getStatus());
        orderService.updateById(order);
        return R.success("派送成功");
    }

}
